package com.lthlearn.controller;

import com.github.pagehelper.PageInfo;
import com.lthlearn.pojo.Result;
import com.lthlearn.pojo.Travel;
import com.lthlearn.service.TravelService;
import com.lthlearn.util.JWTUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @author: 2942733239@qq.com
 * @date: 2024/11/6 15:40
 * @description: com.lthlearn.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("travel")
public class TravelController {

    @Autowired
    private TravelService travelService;

    @GetMapping("searchByLocation")
    public Result searchByLocation(@RequestParam(value = "location") String location) {
        // 1.根据地点查询相关信息
        return travelService.findByName(location);
    }

    /**
     * 根据景点名查找收藏的景点信息
     *
     * @param token
     * @param location
     * @return
     */
    @GetMapping("searchCollectByLocation")
    public Result searchCollectByLocation(@RequestParam(value = "token") String token,
                                          @RequestParam(value = "location") String location) {
        Claims claims = JWTUtils.checkJWT(token);
        // 校验登录状态
        if (claims != null && claims.get("id") != null) {
            // 已登录
            // 1.根据地点查询相关信息
            return travelService.findCollectByName((Integer) claims.get("id"), location);
        }
        // 未登录
        return new Result("500", "查询失败，未登录");
    }

    /**
     * 分页查询所有数据
     *
     * @param pageNum 当前页
     * @return
     */
    @GetMapping("searchAllByPage")
    public Result searchAllByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        // 1.根据地点查询相关信息
        PageInfo<Travel> travelPageList = travelService.findAllByPage(pageNum, 3);
        // 查到返回给前端
        return new Result("200", "查询成功", travelPageList);
    }

}
